package com.zywl.service.impl;

import com.zywl.core.beans.Company;
import com.zywl.core.beans.PayRecord;
import com.zywl.core.beans.Setmeal;
import com.zywl.core.dao.mapper.sys.PayRecordMapper;
import com.zywl.service.CompanyService;
import com.zywl.service.PayRecordService;
import com.zywl.service.SetmealService;
import com.zywl.service.impl.base.BaseServiceImpl;
import com.zywl.utils.models.JsonResult;
import com.zywl.utils.utils.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PayRecordServiceImpl extends BaseServiceImpl<PayRecordMapper,PayRecord> implements PayRecordService {
    @Autowired
    private CompanyService companyService;
    @Autowired
    private SetmealService setmealService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult modifyIsPay(Long bprId)throws Exception{
        JsonResult jr = new JsonResult();
        Date date = new Date();
        PayRecord payRecord = mapper.selectByPrimaryKey(bprId);
        if(payRecord == null){
            return jr.error("支付订单不存在");
        }

        Company company = companyService.getById(payRecord.getPrdCompanyId());
        Setmeal setmeal = setmealService.getById(payRecord.getPrdSetmealId());

        Date endTime = date;
        if(company.getComEndTime() != null && DateUtil.compareDate(date,company.getComEndTime()) < 0){
            endTime = company.getComEndTime();
        }
        if("1".equals(setmeal.getSemUnit())){
            endTime = DateUtil.addDate(endTime,2,setmeal.getSemNum());
        }else if("2".equals(setmeal.getSemUnit())){
            endTime = DateUtil.addDate(endTime,2,setmeal.getSemNum() * 3);
        }else if("3".equals(setmeal.getSemUnit())){
            endTime = DateUtil.addDate(endTime,1,setmeal.getSemNum());
        }
        if(company.getComStartTime() == null){
            company.setComStartTime(date);
        }
        company.setComEndTime(endTime);
        company.setComModifyTime(date);
        company.setComLevel("1");
        company.setComIspay("1");
        companyService.update(company);

        payRecord.setPrdIsuse("1");
        payRecord.setPrdIspay("1");
        payRecord.setPrdPayTime(date);
        payRecord.setPrdModifyTime(date);
        mapper.updateByPrimaryKey(payRecord);

        return jr.success("修改支付状态成功");
    }

}
